<?php
class TypeAction extends  CommonAction {
	/**
	 * 获取模型
	 * @return Model
	 */
	function getModel(){
		return D("Type");
	}
	
	
	function showlist(){
		$action=isset($_REQUEST['action'])?trim($_REQUEST['action']):'list';
		
		switch ($action){
			case 'list':
				$m=$this->getModel();
				//获取所有分类
				$list=$m->order("parent_id asc")->select();
				//echo $m->getLastSql();

				$typelist=array();
				
				foreach ($list as $v){
					if($v['parent_id']==0){
						$typelist[$v['Id']]=$v;
						$typelist[$v['Id']]['sublist']=array();
					}else{
						$typelist[$v['parent_id']]['sublist'][]=$v;
					}
				}
				
				//print_r($typelist);
				
				$this->assign('list',$typelist);
				$this->display("showlist");
				break;
			case 'add':
				if(!$this->isPost()){
					$this->display("edit");
				}else{
					$m=D("Type");
					if($m->create()){
						$id=$m->add();
						if($id){
							$this->assign('jumpUrl',U("/Type/showlist"));
							$this->success("添加成功");
						}else{
							$this->error($m->getDbError());
						}
					}else{
						$this->error($m->getError());
					}
				}
				break;
			case 'edit':
				if(!$this->isPost()){
					$m=$this->getModel();
					$data=$m->getById(intval($_REQUEST['Id']));
					$this->assign('data',$data);
					$this->display('edit');
				}else{
					$m=D("Type");
					if($m->create()){
						$row=$m->save();
						$this->assign('jumpUrl',U("/Type/showlist"));
						$this->success("保存成功");
					}else{
						$this->error($m->getError());
					}
				}
				break;
			case 'remove':
				if(!$this->isPost()){
					$m=$this->getModel();
					$Idlist=trim($_REQUEST['Idlist']);
					$rows=$m->where("Id in ($Idlist) or parent_id in ($Idlist)")->delete();
					$this->success("成功删除");
				}
				break;
		}
	}
}